package com.warm.base.jdk.thread.threadlocal;

import java.util.Date;

public class Actor extends Thread {

    @Override
    public void run() {
        //run方法执行体
        System.out.println(getName() + "是一个演员！");

        int count = 0;
        boolean flag = true;
        while (flag) {
            System.out.println(getName() + "登台演出:" + (++count));

            if (count % 10 == 0) {
                try {
                    System.out.println("线程开始休眠");
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            if (count == 100) {
                flag = false;
            }
        }

        System.out.println(getName() + "的演出结束了！");

    }

    public static void main(String[] args) throws InterruptedException {

            Thread thread0 = new Thread(() -> {
                try {
                    System.out.println(new Date() + "\t" + Thread.currentThread().getName() + "\t太困了，让我睡10秒，中间有事叫我，zZZ。。。");
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    System.out.println(new Date() + "\t" + Thread.currentThread().getName() + "\t被叫醒了，又要继续干活了");
                }
            });
            thread0.start();

            // 这里睡眠只是为了保证先让上面的那个线程先执行
            Thread.sleep(2000);

            new Thread(() -> {
                System.out.println(new Date() + "\t" + Thread.currentThread().getName() + "\t醒醒，醒醒，别睡了，起来干活了！！！");
                // 无需获取锁就可以调用interrupt
                thread0.interrupt();
            }).start();

//        Thread actor = new Actor();
//        actor.setName("Mr.Thread");
//        actor.start();

    }

    //内部类  一个class只允许有一个public修饰
    class ActorMs implements Runnable {

        @Override
        public void run() {
            //run方法执行体
            System.out.println(getName() + "是一个演员！");

            int count = 0;
            boolean flag = true;
            while (flag) {
                System.out.println(getName() + "登台演出:" + (++count));

                if (count % 10 == 0) {
                    try {
                        System.out.println("线程开始休眠");
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                if (count == 100) {
                    flag = false;
                }
            }

            System.out.println(getName() + "的演出结束了！");


        }
    }


}
